﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace XPathAutoSuggest {
    /// <summary>
    /// Class handles IO operations typical for a form that allows user to edit some text and save ti to file
    /// </summary>
    public class FileIOHelper : IFileIoHelper {
        /// <summary>
        /// Writes the users' FormattedXml to file
        /// </summary>
        /// <param name="fileName">File name to save</param>
        /// <param name="formattedXml">Contents of the file</param>
        /// <param name="isDirty">Flag indicating XML has been modified after saving</param>
        /// <param name="statusInfo">Information to display to user</param>
        public void SaveToFile(string formattedXml, string fileName, ref bool isDirty, ref string statusInfo) {
            using (StreamWriter sw = System.IO.File.CreateText(fileName)) {
                sw.Write(formattedXml);
            }
            isDirty = false;
            statusInfo = "Saved XML to " + fileName;
        }

        /// <summary>
        /// Prompts user to supply a name for their file to open
        /// </summary>
        /// <returns>File name if supplied, else empty</returns>
        public string GetOpenFileName() {
            string fileName = string.Empty;
            System.Windows.Forms.OpenFileDialog ofn = new System.Windows.Forms.OpenFileDialog();
            ofn.Filter = "XML Files|*.xml|All Files|*.*";
            System.Windows.Forms.DialogResult res = ofn.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK) {
                fileName = ofn.FileName;
            }
            return fileName;
        }

        /// <summary>
        /// Prompts user to supply a name for their file to open
        /// </summary>
        /// <returns>File name if supplied, else empty</returns>
        public string GetSaveFileName() {
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.Filter = "XML Files|*.xml|All Files|*.*";
            sfd.FileName = "XpathSample1.xml";
            sfd.OverwritePrompt = true;
            System.Windows.Forms.DialogResult res = sfd.ShowDialog();

            if (res == System.Windows.Forms.DialogResult.OK) {
                return sfd.FileName;
            } else {
                return string.Empty;
            }
        }

        /// <summary>
        /// Simple IO method reads the file and returns the contents
        /// </summary>
        /// <param name="inFileName">File name to open</param>
        /// <returns>The file contents if the file exists, otherwise empty string</returns>
        public string ReadFile(string inFileName) {
            if (File.Exists(inFileName)) {
                using (StreamReader sr = System.IO.File.OpenText(inFileName)) {
                    string xml = sr.ReadToEnd();
                    return xml;
                }
            } else
                return string.Empty;
        }
    }
}
